﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScriptifyLibrary20;
using ScriptifyLibrary40;
using System.Xml;
using System.IO;
using Microsoft.SqlServer.Management.Sdk.Sfc;

namespace ScriptifyCommandLine
{
    class Program
    {
        static void Main(string[] args)
        {

            IScriptifyDataAccess scriptify;

            scriptify = new ScriptifySQLServer();

            ScriptifyServer ss = new ScriptifyServer();
            ScriptifyOptions so = new ScriptifyOptions();
            List<ScriptifyObject> objects = new List<ScriptifyObject>();

            ScriptifyConfigReader(so, ss, objects);

            scriptify.ScriptDatabase(ss, so, objects, new EmptyProgress());
        }

        private static void ScriptifyConfigReader(ScriptifyOptions so, ScriptifyServer ss, List<ScriptifyObject> objects)
        {
            using (XmlReader reader = XmlReader.Create("ScriptifyConfig.xml"))
            {
                reader.ReadToFollowing("ScriptifyOptions");
                
                reader.ReadToFollowing("includeHeaders");
                so.includeHeaders = reader.ReadElementContentAsBoolean();

                reader.ReadToFollowing("includeIndexes");
                so.includeIndexes = reader.ReadElementContentAsBoolean();

                reader.ReadToFollowing("includeKeys");
                so.includeKeys = reader.ReadElementContentAsBoolean();

                reader.ReadToFollowing("includeCollation");
                so.includeCollation = reader.ReadElementContentAsBoolean();

                reader.ReadToFollowing("qualifySchema");
                so.qualifySchema = reader.ReadElementContentAsBoolean();

                reader.ReadToFollowing("qualifyForeignKeysSchema");
                so.qualifyForeignKeysSchema = reader.ReadElementContentAsBoolean();

                reader.ReadToFollowing("includePermissions");
                so.includePermissions = reader.ReadElementContentAsBoolean();

                reader.ReadToFollowing("includeDrops");
                so.includeDrops = reader.ReadElementContentAsBoolean();

                reader.ReadToFollowing("includeCreates");
                so.includeCreates = reader.ReadElementContentAsBoolean();

                reader.ReadToFollowing("includeIfNotExists");
                so.includeIfNotExists = reader.ReadElementContentAsBoolean();

                reader.ReadToFollowing("isUnicode");
                so.isUnicode = reader.ReadElementContentAsBoolean();

                reader.ReadToFollowing("folderLocation");
                so.folderLocation = reader.ReadElementContentAsString();

                reader.ReadToFollowing("folderNaming");
                so.folderNaming = reader.ReadElementContentAsString();

                reader.ReadToFollowing("multipleFiles");
                so.multipleFiles = reader.ReadElementContentAsBoolean();

                reader.ReadToFollowing("namingConvention");
                so.namingConvention = reader.ReadElementContentAsBoolean();


                reader.ReadToFollowing("ScriptifyServer");

                reader.ReadToFollowing("serverName");
                ss.serverName = reader.ReadElementContentAsString();

                reader.ReadToFollowing("databaseName");
                ss.databaseName = reader.ReadElementContentAsString();

                reader.ReadToFollowing("username");
                ss.username = reader.ReadElementContentAsString();

                reader.ReadToFollowing("password");
                ss.password = reader.ReadElementContentAsString();

                reader.ReadToFollowing("isWindowsAuthentication");
                ss.isWindowsAuthentication = reader.ReadElementContentAsBoolean();

                reader.ReadToFollowing("ScriptifyObjects");
                while (reader.ReadToFollowing("object"))
                {
                    ScriptifyObject obj = new ScriptifyObject();
                    reader.MoveToNextAttribute();
                    obj.ObjectType = reader.Value;
                    reader.MoveToNextAttribute();
                    obj.Schema = reader.Value;
                    reader.MoveToNextAttribute();
                    obj.ObjectName = reader.Value;
                    reader.MoveToNextAttribute();
                    obj.URN = reader.Value;
                    objects.Add(obj);
                }
            }
        }
    }
}
